package com.bugstack.domain.strategy.service.rule.tree.factory.engine.impl;

import com.bugstack.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import com.bugstack.domain.strategy.model.vo.RuleTreeNodeLineVO;
import com.bugstack.domain.strategy.model.vo.RuleTreeNodeVO;
import com.bugstack.domain.strategy.model.vo.RuleTreeVO;
import com.bugstack.domain.strategy.service.rule.tree.ILogicTree;
import com.bugstack.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import com.bugstack.domain.strategy.service.rule.tree.factory.engine.IDecisionTreeEngine;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: czb
 * @Description: 规则树引擎
 * @DateTime: 2025/3/10 16:10
 **/

@Slf4j
public class DecisionTreeEngine implements IDecisionTreeEngine {

    private final Map<String, ILogicTree> logicTreeNodeMap;

    private final RuleTreeVO ruleTreeVO;

    public DecisionTreeEngine(Map<String, ILogicTree> logicTreeNodeGroup, RuleTreeVO ruleTreeVO) {
        this.logicTreeNodeMap = logicTreeNodeGroup;
        this.ruleTreeVO = ruleTreeVO;
    }


    @Override
    public DefaultTreeFactory.StrategyAwardVO process(String userId, Long strategyId, Integer awardId, Date endDateTime) {
        DefaultTreeFactory.StrategyAwardVO awardData = null;

        //取出第一个规则节点
        String nextNode = ruleTreeVO.getTreeRootRuleNode();
        Map<String, RuleTreeNodeVO> treeNodeMap = ruleTreeVO.getTreeNodeMap();

        while (null != nextNode) {
            RuleTreeNodeVO ruleTreeNodeVO = treeNodeMap.get(nextNode);
            ILogicTree logicTreeNode = logicTreeNodeMap.get(ruleTreeNodeVO.getRuleKey());
            String ruleValue = ruleTreeNodeVO.getRuleValue();
            DefaultTreeFactory.TreeActionEntity actionEntity = logicTreeNode.logic(userId, strategyId, awardId, ruleValue, endDateTime);
            RuleLogicCheckTypeVO ruleLogicCheckTypeVO = actionEntity.getRuleLogicCheckType();
            awardData = actionEntity.getStrategyAwardVO();
            log.info("决策树引擎【{}】treeId:{} node:{} code:{}", ruleTreeVO.getTreeName(), ruleTreeVO.getTreeId(), nextNode, ruleLogicCheckTypeVO.getCode());
            nextNode = nextNode(ruleLogicCheckTypeVO.getCode(), ruleTreeNodeVO.getTreeNodeLineVOList());
        }

        return awardData;
    }

    private String nextNode(String matterValue, List<RuleTreeNodeLineVO> treeLineList) {
        if (null == treeLineList || treeLineList.isEmpty())  return null;

        for (RuleTreeNodeLineVO line : treeLineList) {
            if (decisionLogic(matterValue, line)) {
                return line.getRuleNodeTo();
            }
        }

//        throw new RuntimeException("决策树引擎，nextNode 计算失败，未找到可执行节点！");
        return null;
    }


    public boolean decisionLogic(String matterValue, RuleTreeNodeLineVO nodeLine) {
        switch (nodeLine.getRuleLimitType()) {
            case EQUAL:
                return matterValue.equals(nodeLine.getRuleLimitValue().getCode());
            // 以下规则暂时不需要实现
            case GT:
            case LT:
            case GE:
            case LE:
            default:
                return false;
        }
    }
}
